package com.lcz.web;

import com.lcz.bean.User;
import com.lcz.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")//这里必须有/,不然全崩了

public class LoginServlet extends HttpServlet {

    UserDao userDao = new UserDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = userDao.login(username, password);
        if(user == null){
            //req,resp
            resp.sendRedirect("fail.jsp");
        }else {
            //使用Session 存储数据
            req.getSession().setAttribute("user",user);
            req.getSession().setAttribute("username",username);

            resp.sendRedirect("success.jsp");
        }
    }
}
